package com.yjck.ijpay.pojo;


import com.github.pagehelper.PageInfo;
import com.yjck.ijpay.config.CommonConstant;

import java.io.Serializable;
import java.util.List;


/**
 * 响应的基类
 */
public class BaseResponse<T> implements Serializable {

    private static final long serialVersionUID = -1011440293454989799L;
    private String status;
    private String detail;
    private Object data;

    private BaseResponse(String status, T data, String detail) {
        super();
        this.status = status;
        this.data = data;
        this.detail = detail;
    }

    public static BaseResponse result(int result) {
        if (CommonConstant.INSERT_AND_UPDATE_RETURN < result) {
            return success();
        }
        return error();
    }

    public static BaseResponse result(boolean result) {
        if (result) {
            return success();
        }
        return error();
    }

    public static BaseResponse success() {
        return newInstance(Constant.CommonCode.SUCCESS_CODE, "", Constant.CommonMessage.SUCCESS_MESSAGE);
    }

    public static BaseResponse success(Object data) {
        return newInstance(Constant.CommonCode.SUCCESS_CODE, data, Constant.CommonMessage.SUCCESS_MESSAGE);
    }

    public static BaseResponse error() {
        return newInstance(Constant.CommonCode.ERROR_CODE, "", Constant.CommonMessage.ERROR_MESSAGE);
    }

    public static BaseResponse error(String msg) {
        return newInstance(Constant.CommonCode.ERROR_CODE, "", msg);
    }

    public static <T> BaseResponse<T> error(T data, String detail) {
        return newInstance(Constant.CommonCode.ERROR_CODE, data, detail);
    }

    public static BaseResponse paramError() {
        return newInstance(Constant.CommonCode.PARAM_ERROR_CODE, "", Constant.CommonMessage.PARAM_ERROR_MESSAGE);
    }

    public static <T> BaseResponse paramError(String msg) {
        return newInstance(Constant.CommonCode.PARAM_ERROR_CODE, "", msg);
    }

    public BaseResponse(String status) {
        this.status = status;
    }

    public BaseResponse(String status, T data) {
        this.status = status;
        this.setData(data);
    }

    public static <T> BaseResponse newInstance(String status) {
        return new BaseResponse(status);
    }

    public static <T> BaseResponse<T> newInstance(String status, T data) {
        return new BaseResponse(status, data);
    }

    public static <T> BaseResponse<T> newInstance(String status, T data, String detail) {
        return new BaseResponse(status, data, detail);
    }

    public String getStatus() {
        return status;
    }

    public BaseResponse setStatus(String status) {
        if (null == status)
            this.status = "";
        else
            this.status = status;
        return this;
    }

    public Object getData() {
        return data;
    }

    public String getDetail() {
        return detail;
    }

    /**
     * 设置 响应数据 列表类型数据(list)会自动转换为分页类型的数据
     *
     * @param data
     * @return
     */
    @SuppressWarnings({"unchecked", "rawtypes"})
    public BaseResponse setData(T data) {
        if (data instanceof List) {
            this.data = new PageInfo<>((List) data);
        } else {
            this.data = data;
        }
        return this;
    }

    public BaseResponse setDetail(String message) {
        if (null == message)
            this.detail = "";
        else
            this.detail = message;
        return this;
    }

}
